package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.BootstrapText;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.AQIInfoE;
import com.infore.airsupervise.bean.QuestionE;
import com.infore.airsupervise.biz.QuestionDealBiz;
import com.infore.airsupervise.biz.impl.QuestionDealBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.ItemDecoration1px;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.ui.widget.refresh.swipetoloadlayout.OnLoadMoreListener;
import com.infore.airsupervise.ui.widget.refresh.swipetoloadlayout.OnRefreshListener;
import com.infore.airsupervise.ui.widget.refresh.swipetoloadlayout.SwipeToLoadLayout;
import com.infore.airsupervise.utils.DateUtil;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 提交的问题上报列表
 */
public class QuestionListActivity extends BaseTitleBarActivity implements OnRefreshListener, OnLoadMoreListener, SwipeRecyclerView.OnActionListener {

    @BindView(R.id.refreshRecyclerView)
    SwipeRecyclerView swipeRecyclerView;

    private BaseQuickAdapter<QuestionE,BaseViewHolder> adapter;//适配器
    private List<QuestionE> list = new ArrayList<>();//列表集合

    private QuestionDealBiz questionDealBiz;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_list);
        ButterKnife.bind(this);
        questionDealBiz = new QuestionDealBizImpl();
        initTitleBar();
        initRecycleView();
    }

    private void initTitleBar(){
        title.setText("我提交的问题上报");
        rightMenu.setFontAwesomeIcon(FontAwesome.FA_PLUS);
    }

    @Override
    protected void onRightMenuClick(View view) {
        JumpUtil.ForResultJumpFromTo(QuestionListActivity.this, QuestionCreateActivity.class,200);
    }

    /**
     * 设置列表和适配器
     */
    private void initRecycleView() {
        //适配器
        adapter = new BaseQuickAdapter<QuestionE, BaseViewHolder>(R.layout.item_inspect_plan, list) {
            @Override
            protected void convert(BaseViewHolder helper, QuestionE item) {
                helper.setText(R.id.tv_time, DateUtil.toShortDate(item.getReleaseTime()))
                        .setText(R.id.tv_title, item.getQuestionTitle())
                        .setText(R.id.tv_content, item.getQuestionAddress());
            }
        };
        swipeRecyclerView.setAdapter(adapter);
        swipeRecyclerView.setActionListener(this);
        // 首次自动刷新
        swipeRecyclerView.getSwipeLayout().setRefreshing(true);
        swipeRecyclerView.onRefresh();
        //

    }




    /**
     * 获取问题报告列表
     */
    private void getData(final boolean isRefresh){
        questionDealBiz.sendQuestionListRequest(new OnServerResponseListener<List<QuestionE>>() {
            @Override
            public void onSucceed(List<QuestionE> result, String desc) {
                swipeRecyclerView.commonSuccessHandle(isRefresh,result.size());
                if(isRefresh){
                    list.clear();
                }
                list.addAll(result);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                swipeRecyclerView.commonFailedHandle(isRefresh, errorCode, title, desc);
            }
        });
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRecyclerView.getAdapter().loadMoreEnd();
                swipeRecyclerView.getSwipeLayout().setEnabled(true);
            }
        }, 1000);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        JumpUtil.jumpFromTo(ctx,QuestionDetailActivity.class, Constants.INTENT_OBJ_KEY,list.get(position));
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        getData(true);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==200 && resultCode == RESULT_OK){
            swipeRecyclerView.getSwipeLayout().setRefreshing(true);
            swipeRecyclerView.onRefresh();
        }
    }
}
